//
//  Pet_MineViewController.swift
//  PetApp
//
//  Created by mac on 2023/9/18.
//  Copyright © 2023 Pet_. All rights reserved.
//

import UIKit

class Pet_MineViewController: BasicViewController {

    let topV = UIImageView.initImgView(img: "topV")
    let avatarB = UIButton.initV(img: "avarB")
    let arr = ["Friends","My troubles","Blocklist","Feedback","Setting"]
    private lazy var table: UITableView = {
        let view = UITableView(frame: .zero, style: .plain)
        view.separatorStyle = .none
        view.backgroundColor = .clear
        view.delegate = self
        view.dataSource = self
        view.showsVerticalScrollIndicator = false
        view.register(MineTableViewCell.self, forCellReuseIdentifier: "cell")
        view.tableHeaderView = self.headerV
        return view
    }()
    
    let nameL = UILabel.initV(UIFont.setTypeFace(24,type: .bold), text: "", textColor: titleColor)
    let idL = UILabel.initV(UIFont.setTypeFace(14,type: .bold), text: "", textColor: titleColor)
    let descL = UILabel.initV(UIFont.setTypeFace(14,type: .bold), text: "", textColor: titleColor)

    let rechrgeB = UIButton.initV(img: "")
    let rechrgeL = UILabel.initV(UIFont.setTypeFace(11,type: .bold), text: "My coin", textColor: mainColor)
    let coinsL = UILabel.initV(UIFont.setTypeFace(25,type: .bold), text: "0", textColor: mainColor)
    let recL = UILabel.initV(UIFont.setTypeFace(19,type: .extraBold), text: "Recharge", textColor: .black)
    var petDataSource:[InfoUtil.petDta] = []

    lazy var headerV: UIView = {
        let v = UIView.init(frame: .init(x: 0, y: 0, width: screenWidth, height: 350.Fit))
        v.isUserInteractionEnabled = true
        v.subviews {
            topV
            idL
            avatarB
            nameL
            descL
            rechrgeB.subviews {
                rechrgeL
                coinsL
                recL
            }
        }
        
        topV.left(0.Fit).top(0).right(0.Fit).bottom(15.0.Fit)
        avatarB.size(122.Fit).top(NavHeight).right(15.Fit)
        avatarB.setAllCorners(corner: 35.Fit)
        avatarB.addAct {
            let vc = Fish_EditInfoViewController()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
        nameL.left(15.Fit).height(30.Fit).top(NavHeight)
        nameL.Trailing == avatarB.Leading - 30.Fit
        
        idL.left(15.Fit).height(30.Fit).width(200.Fit)
        idL.Top == nameL.Bottom + 5.Fit

        descL.left(15.Fit).height(30.Fit).width(200.Fit)
        descL.Top == idL.Bottom + 5.Fit
        descL.numberOfLines = 0

        rechrgeB.left(15.Fit).right(15.Fit).height(88.Fit)
        rechrgeB.Top == avatarB.Bottom + 15.Fit
        rechrgeB.setBackgroundImage(.init(named: "rechargeB"), for: .normal)
        rechrgeB.addAct {
            let vc = Pet_RechargeViewController()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
        coinsL.height(25.Fit).width(100.Fit).left(25.Fit).top(22.Fit)
        rechrgeL.left(20.Fit).height(25.Fit).width(80)
        rechrgeL.Top == coinsL.Bottom
        
        recL.backgroundColor = .color(hexString: "#E5F789")
        recL.right(25.Fit).height(40.Fit).width(113.Fit)
        recL.setAllCorners(corner: 14.Fit)
        recL.centerVertically()
        recL.textAlignment = .center
        
        return v
    }()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        table.contentInsetAdjustmentBehavior = .never;
        RT.getUserInfoUrlInfoWith { info in
            if let url = URL(string: USEINFOMODEL.headImage ?? "") {
                self.avatarB.kf.setImage(with: url, for: .normal, placeholder: .init(named: "avarB"))
            }
            self.nameL.text  = USEINFOMODEL.name
            self.coinsL.text  = USEINFOMODEL.coins
            self.idL.text = "ID: " + (USEINFOMODEL.id ?? "")
            self.navigationController?.navigationBar.isHidden = true
        }
        petDataSource.removeAll()
        if let str = USEINFOMODEL.tags,str.count != 0 {
            let arr = str.components(separatedBy: "@@")
            for dict in arr{
                if let d = InfoUtil.petDta.deserialize(from: dict){
                    petDataSource.append(d)
                }
            }
            self.table.reloadData()
        }

    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.subviews {
            table
        }
        table.left(0).right(0).top(0).bottom(TabbarHeight)
    }
}

extension Pet_MineViewController:UITableViewDelegate,UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80.Fit
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 450.Fit
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MineTableViewCell
        cell.selectionStyle = .none
        cell.nameLabel.text = arr[indexPath.row]
        cell.morelabel.isHidden = true
        cell.iconI.image = .init(named: arr[indexPath.row])
        cell.more.isHidden = false
        return cell
    }
            
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let arr = ["Friends","My troubles","Blocklist","Feedback","Setting"]
        switch arr[indexPath.row] {
        case "My troubles":
            break
        case "Feedback":
            let vc = Explore_FeedBackViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            break
        case "Friends":
            let vc = Pet_FollowViewController()
            vc.type = 0
            self.navigationController?.pushViewController(vc, animated: true)
            break
        case "Setting":
            let vc = Plant_SettingViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            break
        case "Blocklist":
            let vc = Pet_BlackViewController()
            vc.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vc, animated: true)
            break
        default:
            break
        }
    }
    
    func clearCache() {
        Toast_View_Tool.show()
        // 取出cache文件夹目录
        let cachePath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first
        let fileArr = FileManager.default.subpaths(atPath: cachePath!)
        // 遍历删除
        for file in fileArr! {
            
            let path = (cachePath! as NSString).appending("/\(file)")
            if FileManager.default.fileExists(atPath: path) {
                do {
                    try FileManager.default.removeItem(atPath: path)
                } catch {
                }
            }
        }
        Toast_View_Tool.show(type: .success,text: "Clean up completed!")
    }
    
}


class MineTableViewCell: UITableViewCell {
    
    let more = UIImageView.initImgView(img: "more")
    let nameLabel = UILabel.initV(.setTypeFace(18,type: .bold), text: "", textColor: mainColor)
    let morelabel = UILabel.initV(.setTypeFace(18,type: .bold), text: "", textColor: .white)
    let backV = UIView.init(frame: .zero)
    let iconI = UIImageView.initImgView(img: "")
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        selectionStyle = .none
        
        contentView.subviews {
            backV.subviews {
                iconI
                nameLabel
                more
                morelabel
            }
        }
        
        backV.left(15.Fit).right(15.Fit).top(15.Fit).bottom(0)
        backV.setAllCorners(corner: 10.Fit)
        backV.backgroundColor = .init(white: 1, alpha: 0.15)
        
        iconI.size(50.Fit).left(15.Fit)
        iconI.centerVertically()
        
        more.width(12.Fit).height(22.Fit).right(15)
        more.centerVertically()
        
        morelabel.height(25.Fit).width(55.Fit).right(15.Fit)
        morelabel.centerVertically()
        
        nameLabel.Leading == iconI.Trailing + 15.Fit
        nameLabel.top(15).height(30)
        nameLabel.Trailing == more.Leading - 20
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


class CardPetCell: UITableViewCell {
    
    let more = UIImageView.initImgView(img: "more")
    let nameLabel = UILabel.initV(.setTypeFace(18,type: .bold), text: "", textColor: titleColor)
    let morelabel = UILabel.initV(.setTypeFace(18,type: .bold), text: "", textColor: .white)

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        selectionStyle = .none
        
        contentView.subviews {
            nameLabel
            more
            morelabel
        }
        
        more.size(40).right(15)
        more.centerVertically()
        
        morelabel.height(25.Fit).width(55.Fit).right(15.Fit)
        morelabel.centerVertically()
        
        nameLabel.left(20.Fit).top(15).height(30)
        nameLabel.Trailing == more.Leading - 20
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

